package com.csun.cmny.provider.controller;

import cn.jiguang.common.utils.Preconditions;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcExpenseTypeModelCode;
import com.csun.cmny.provider.model.dto.ExpenseTypeModelCodeDto;
import com.csun.cmny.provider.model.vo.ExpenseTypeModelCodeVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcExpenseTypeModelCodeService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping(value = "/expenseTypeModelCode", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcExpenseTypeModelCodeController extends BaseController {

    @Resource
    private OmcExpenseTypeModelCodeService omcExpenseTypeModelCodeService;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    /**
     * 费用类别配置-添加
     * @param: [expenseTypeModelCodeDto, loginAuthDto]
     * @return: java.lang.Integer
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/add")
    public Wrapper addExpenseTypeModelCode(
                                  @RequestBody List<ExpenseTypeModelCodeDto> expenseTypeModelCodeDtoList, BindingResult br){

        List<OmcExpenseTypeModelCode> omcExpenseTypeModelCodeList = new LinkedList<>();
        if(expenseTypeModelCodeDtoList != null && expenseTypeModelCodeDtoList.size() > 0){
            for(ExpenseTypeModelCodeDto dto : expenseTypeModelCodeDtoList){
                omcExpenseTypeModelCodeList.add(convert(dto));
            }
        }

        int ret = omcExpenseTypeModelCodeService.addExpenseTypeModelCode(omcExpenseTypeModelCodeList,getLoginAuthDto());

//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), ret);
    }

    /**
     * 改变(费用是否可调整)配置
     * @param: [id]
     * @return: java.lang.Integer
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/changeIsAdjust/{id}")
    public Wrapper changeIsAdjust(@PathVariable Long id){

        Preconditions.checkArgument(id != null, cmnyMessageSource
                .getMessage("ExpenseTypeModelCode.id.null", null));
        int ret = omcExpenseTypeModelCodeService.changeIsAdjust(id);

//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), ret);
    }

    /**
     * 改变(出院是否退费)配置
     * @param: [id]
     * @return: java.lang.Integer
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/changeIsOutReturn/{id}")
    public Wrapper changeIsOutReturn(@PathVariable Long id){

        Preconditions.checkArgument(id != null, cmnyMessageSource
                .getMessage("ExpenseTypeModelCode.id.null", null));
        int ret = omcExpenseTypeModelCodeService.changeIsOutReturn(id);

//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), ret);
    }

    /**
     * 改变(请假是否退费)配置
     * @param: [id]
     * @return: java.lang.Integer
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/changeIsLeaveReturn/{id}")
    public Wrapper changeIsLeaveReturn(@PathVariable Long id){

        Preconditions.checkArgument(id != null, cmnyMessageSource
                .getMessage("ExpenseTypeModelCode.id.null", null));
        int ret = omcExpenseTypeModelCodeService.changeIsLeaveReturn(id);

//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), ret);
    }

    /**
     * 删除配置
     * @param: [id]
     * @return: java.lang.Integer
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/deleteById/{id}")
    public Wrapper deleteById(@PathVariable Long id){

        Preconditions.checkArgument(id != null, cmnyMessageSource
                .getMessage("ExpenseTypeModelCode.id.null", null));
        int ret = omcExpenseTypeModelCodeService.deleteById(id);

//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), ret);
    }

    /**
     * 费用类别配置-查询
     * @param: []
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/queryAll")
    public Wrapper queryAll(@RequestBody ExpenseTypeModelCodeDto expenseTypeModelCodeDto){

        List<ExpenseTypeModelCodeVo> ret = omcExpenseTypeModelCodeService.queryAll(expenseTypeModelCodeDto,getLoginAuthDto());

//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), ret);
    }

    /**
     * 转换
     * @param: [expenseTypeModelCodeDto]
     * @return: com.csun.cmny.provider.model.domain.OmcExpenseTypeModelCode
     */
    public OmcExpenseTypeModelCode convert(ExpenseTypeModelCodeDto expenseTypeModelCodeDto){
        OmcExpenseTypeModelCode omcExpenseTypeModelCode = new OmcExpenseTypeModelCode();
        BeanUtils.copyProperties(expenseTypeModelCodeDto,omcExpenseTypeModelCode);
        return omcExpenseTypeModelCode;
    }

}
